package com.yiqiu.common.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yiqiu.common.core.domain.entity.BaseEntity;
import com.yiqiu.common.core.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;
import java.util.Objects;

/**
 * @author weiyi
 */
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始插入处理");
        if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
            BaseEntity object = (BaseEntity) metaObject.getOriginalObject();
            Date nowData = new Date();

            if (StringUtils.isNull(object.getCreateBy())) {
                object.setCreateBy("1");
            }
            if (StringUtils.isNull(object.getCreateTime())) {
                object.setCreateTime(nowData);
            }
            if (StringUtils.isNull(object.getUpdateBy())) {
                object.setUpdateBy("1");
            }
            if (StringUtils.isNull(object.getUpdateTime())) {
                object.setUpdateTime(nowData);
            }
        }

    }


    @Override
    public void updateFill(MetaObject metaObject) {
        if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
            BaseEntity object = (BaseEntity) metaObject.getOriginalObject();
            Date nowData = new Date();
            object.setUpdateBy("1");
            object.setUpdateTime(nowData);

        }

    }


}
